// pages/homePage/homePage.js
var {
  Api
} = require('../../api/api')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemsindex: -1,
    ti: [['0', 20, 30, 40, 50, 60, 70, 80], ['题']],
    tiindex: [0, 0],
    items: [],//我的题库
    current: 0,//轮播图的索引
    page: 1,
    page_size: 200,
    list: [],//课程列表
    kong: false,//我的题库是否为空
    blackid: 0,//刷题大厅页面传过来的id
    tian:'',//几天刷完-1表示已经刷完
    viptrue:false,
    domain: Api.domain,
    register: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      register:  Boolean(!wx.$cache.get('key'))
    })
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  
  },
  bindchinge(e) {
    console.log(e)
    this.setData({
      current: e.detail.current,
      tian:''
    })
    this.mykelist()
    this.titong()
  },
  // flage(e) {//展开与不展开
  //   let { index } = e.currentTarget.dataset
  //   if (index == this.data.itemsindex) {
  //     this.setData({
  //       itemsindex: -1
  //     })
  //   } else {
  //     this.setData({
  //       itemsindex: index
  //     })
  //   }
  // },
  bindPickerChange(e) {
    this.setData({
      tiindex: e.detail.value
    })
    this.setting()
  },
  shoushi(){
  
    this.setData({
      shoushishow:false
    })
    wx.$cache.set('shoushishow',true,1850000)
  },
  goDa() {//去刷题大厅
    let index = 1
    if (this.data.items.length != 0) {
      index = 0
    }
    wx.navigateTo({
      url: `/pages/homehall/homehall?index=${index}`,
    })
  },
  myti() {//我的题库
    let params = {
      page: 1,
      page_size: 100
    }
    wx.$http.get(wx.$get.my_library, params).then(res => {
      console.log(res, '我的题库')
      if(!wx.$cache.get('shoushishow')&&res.length>1){
        this.setData({
          shoushishow:true
        })
      }
      this.setData({
        items: res
      })

     console.log("8888888888877777777777733333",this.data.items);

      if (res.length == 0) {
        this.setData({
          kong: true
        })
      } else {
        this.setData({
          kong: false
        })
        if (this.data.blackid) {//刷题大厅点击题库跳转过来的判断
          let arr = []
          for (let i = 0; i < res.length; i++) {
            arr = arr.concat(res[i].id)
          }
          let current = arr.indexOf(this.data.blackid)
          this.setData({
            current
          })
          this.setData({
            blackid:false
          })
        }
        this.mykelist()
        this.titong()
      }
    })
  },
  mykelist() {//课程列表
    let params = {
      member_id: wx.$cache.get('member_id'),
      library_id: this.data.items[this.data.current].id,
      page: this.data.page,
      page_size: this.data.page_size
    }
    wx.$http.get(wx.$get.library_index_list, params).then(res => {
      console.log(res, '课程列表')
     
      this.setData({
        list: res
      })
    })
  },
  bindflage(e) {//展开与合并
    let { index } = e.currentTarget.dataset
    this.setData({
      [`list[${index}].flage`]: !this.data.list[index].flage
    })
  },
  bindcong() {//重新刷题
    if(this.data.items[this.data.current].is_vip==1&&!this.data.viptrue){
      wx.navigateTo({
        url: '/pages/memberCenter/memberCenter',
      })
      return
     }
    wx.showModal({
      title: '提示',
      content: '你确定要重新刷题吗？',
      success: (res) => {
        if (res.confirm) {
          let params = {
            library_id: this.data.items[this.data.current].id
          }
          wx.$http.get(wx.$get.again_subject, params).then(res => {
            console.log(res, '重新刷题')
            this.myti()
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  goti(e) {//去做题
    let { zid, kid, tid, num ,nzid,nkid,ntid,nzids } = e.currentTarget.dataset
    //num有个问题需要判断一一下默认为n-1当为0的时候还是0 
    if (num != 0) {
      num = Number(num) - 1
    }
if(this.data.items[this.data.current].is_vip==1&&!this.data.viptrue){
  wx.navigateTo({
    url: '/pages/memberCenter/memberCenter',
  })
}else{
 //将，本课程的 题库，信息，章节信息全部传递过去
  
  if(typeof(nzids)==='undefined'){
    nzids={};
  }
  let nzidss=JSON.stringify(nzids)
  wx.navigateTo({
    url: `/pages/brushTopic/brushTopic?zid=${zid}&kid=${kid}&tid=${tid}&type=1&num=${num}&nzids=${nzidss}`,
  })
}

  },
  bindshui() {//开始考试
    if(this.data.items[this.data.current].is_vip==1&&!this.data.viptrue){
      wx.navigateTo({
        url: '/pages/memberCenter/memberCenter',
      })
      return
     }
    wx.navigateTo({
      url: `/pages/brushTopic/brushTopic?tid=${this.data.items[this.data.current].id}&type=2`,
    })
  },
  go(e) {
    let { url } = e.currentTarget.dataset
    wx.navigateTo({
      url,
    })
  },
  titong() {//题库主页统计
    let params = {
      member_id: wx.$cache.get('member_id'),
      library_id: this.data.items[this.data.current].id,
    }
    wx.$http.get(wx.$get.library_index, params).then(res => {
      console.log(res, '题库主页统计')
      let num = res.library_list.subject_num
      let arr = []
      for (let i = 1; i < num; i++) {
        if(i!=num){
          if (i % 10 == 0) {
            arr = arr.concat(i)
          }
        }
      }
      arr = arr.concat(num)
      this.setData({
        [`ti[0]`]:arr
      })
      if(res.library_target){
        let tiindex= arr.indexOf(res.library_target.target_num)
        this.setData({
          'tiindex[0]':tiindex,
        })
        if(res.speed_log.total_num>=res.library_list.subject_num){
          this.setData({
            tian:-1
          })
        }else{
          this.setData({
            tian:Math.ceil((res.library_list.subject_num-res.speed_log.total_num)/res.library_target.target_num)
          })
        }
      }
    })
  },
  setting(){//设置每日刷题量
    let params = {
      target_num:this.data.ti[0][this.data.tiindex[0]],
      library_id: this.data.items[this.data.current].id,
    }
    wx.$http.get(wx.$get.library_target,params).then(res=>{
      console.log(res,'每日刷体谅')
      this.titong()
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log(Boolean(!wx.$cache.get('key')));
    if (wx.$cache.get('userinfo')) {
      let viptime=new Date(wx.$cache.get('userinfo').vip_time.replace(/\-/g, '/')).getTime();
      let today=new Date().getTime()
      console.log(today)
      let viptrue=false
      if(viptime>today){
        viptrue=true
        this.setData({
          viptrue,
        })
      }
      this.myti()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return{
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      path:`/pages/index/index?pid=${wx.$cache.get('member_id')}`
    }
  },
  onShareTimeline(){
    return {
      title:wx.$cache.get('fx').title,
      imageUrl:wx.$cache.get('fx').img,
      query:`a=${wx.$cache.get('member_id')}`,
    }
  }
})
